﻿#define WIN32_LEAN_AND_MEAN//去除windows的部分引用
#define _WINSOCK_DEPRECATED_NO_WARNINGS 
//inet_addr该函数在新的MFC中被替换，可以在vs的属性中设置预处理宏来使用该函数，防止报错

#include <iostream>
#include <windows.h>
#include <winsock2.h>

#pragma comment(lib,"ws2_32.lib")

int main()
{
	WORD _version;
	WSADATA _wsaData;
	int err;

	_version = MAKEWORD(2, 2);

	err = WSAStartup(_version, &_wsaData);
	if (err != 0) {
		return 0;
	}
	if (LOBYTE(_wsaData.wVersion) != 2 || HIBYTE(_wsaData.wVersion != 2)) {
		WSACleanup();
			return 0;
	}
	SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0);

	SOCKADDR_IN addrSrv;
	addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
	addrSrv.sin_family = AF_INET;
	addrSrv.sin_port = htons(6000);
	//char Data[1024];
	/*std::cout<<"
	std::cin.get(Data, 1024);*/
	connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
	send(sockClient, "this is a Server", strlen("this is a Server") + 1, 0);
	char recvBuf[50];
	recv(sockClient, recvBuf, 50, 0);
	printf("%s\n", recvBuf);

	closesocket(sockClient);
	WSACleanup();
}